set matsize 8000
set more off
clear all

cap log close
log using cr_tfp,t replace

*** Program to create a file for TFP estimation in TSP
*** Also runs verious estimators of TFP in stata and saves the predictions

use sample, clear

egen x = max(year),by(id)
gen exit = x==year
tab exit
drop x

keep id year ind logs logm loga loge logi logelec exit tm* firstyr*
do cleanfile
saveold sample_tsp, replace version(12)

xtset id year 
tab year,gen(y_)
global yrdum y_2-y_10
tab ind,gen(i_)
global inddum i_2-i_18

reg logs loga logm loge $yrdum, vce(cluster id)
predict tfp, resid
lincom 1 - loga - loge - logm
est store olsest

opreg logs, exit(exit) state(loga) proxy(logi) free(logm loge) cvars($yrdum) vce(bootstrap, reps(50))
predict tfp_op
lincom 1 - loga - loge - logm
est store opest

levpet logs, free(loge $yrdum) proxy(logm) capital(loga) revenue reps(50) 
predict tfp_lp, omega
lincom 1 - loga - loge - logm
est store lpest

esttab *est, keep(loga loge logm)              ///
       b(%10.4f) se(%10.4f) star stats(r2 rmse F df_m N N_clust) nogaps

acfest logs, state(loga) proxy(logi)    free(loge $yrdum)      nbs(50) robust invest intmat(logm) 
predict tfp_rev_inv, omega
lincom 1 - loga - loge - logm

acfest logs, state(loga) proxy(logm)    free(loge $yrdum)      nbs(50) robust
predict tfp_acf, omega
lincom 1 - loga - loge - logm

acfest logs, state(loga) proxy(logelec) free(loge logm $yrdum) nbs(50) robust
predict tfp_rev_elec, omega
lincom 1 - loga - loge - logm

drop y_* i_*
corr tfp*

do cleanfile
sum
save tfp, replace

log close
